//
//  HeroName.m
//  LOL_英雄介绍模块
//
//  Copyright (c) 2016年 Cry4tal. All rights reserved.
//

#import "GradualLabel.h"

@interface GradualLabel()

@property (strong, nonatomic) CAGradientLayer *gradientLayer;
@property (nonatomic, strong) id superView;

@end

@implementation GradualLabel


-(id)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {

        [self initReferLabel];
        [self setStartColor:[UIColor colorWithRed:213.0/255 green:183.0/255 blue:134.0/255 alpha:1]
                andEndColor:[UIColor colorWithRed:183.0/255 green:74.0/255 blue:23.0/255 alpha:1]
              andStartPoint:CGPointMake(0, 0.3)
                andEndPoint:CGPointMake(0, 1)];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawText{
    
    [self addGradientLayerWithColors];
}

- (void)initReferLabel{

    self.referLabel = [[UILabel alloc] init];
    self.referLabel.text = @"GradualLabel";
    self.referLabel.font = [UIFont boldSystemFontOfSize:18];
    [self.referLabel sizeToFit];
    [self addSubview:self.referLabel];
}

- (void)setStartColor:(UIColor *)startColor andEndColor:(UIColor *)endColor andStartPoint:(CGPoint)startPoint andEndPoint:(CGPoint)endPoint{
    
    self.startPoint = startPoint;
    self.endPoint = endPoint;
    self.startColor = startColor;
    self.endColor = endColor;
}

- (void)addGradientLayerWithColors{
    
    self.referLabel.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);
    // 创建渐变层
    CAGradientLayer * gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.referLabel.frame;
    //更改颜色
    gradientLayer.colors = @[(__bridge id)self.startColor.CGColor,
                             (__bridge id)self.endColor.CGColor];
    //更改渐变方向以及偏移量
    gradientLayer.startPoint = self.startPoint;
    gradientLayer.endPoint = self.endPoint;
    [self.layer addSublayer:gradientLayer];
    gradientLayer.mask = self.referLabel.layer;
    self.referLabel.frame = gradientLayer.bounds;
}

@end
